package cn.dhnep.demo.controller;


import cn.dhnep.demo.pojo.Account;
import cn.dhnep.demo.pojo.Earnings;
import cn.dhnep.demo.pojo.Feedback;
import cn.dhnep.demo.pojo.Recharge;
import cn.dhnep.demo.service.FeedbackService;
import cn.dhnep.demo.util.Page;
import cn.dhnep.demo.util.ResultMap;
import io.swagger.annotations.ApiOperation;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author dhn
 * @since 2021-08-30
 */
@RestController
@RequestMapping("feedback")
public class FeedbackController {

    @Resource
    private FeedbackService feedbackService;

    private Map<String,Object> result = new HashMap<>();


    @ApiOperation("用户意见反馈查询")
    @RequestMapping("feedbacklist")
    public ResultMap<List<Feedback>> Feedbacklist(Page page, @RequestParam("limit") Integer limit){
        page.setRows(limit);
        List<Feedback>feedbackList=feedbackService.selectPageList(page);
        int totals=feedbackService.selectPageCount(page);
        page.setTotalRecord(totals);
        return new ResultMap<List<Feedback>>("",feedbackList,0,totals);
    }

    @ApiOperation("id反馈详情")
    @RequestMapping("idqueryFeedback")
    public void IdqueryFeedback(Integer id, HttpSession session, HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        Feedback feedback =new Feedback();
        feedback.setId(id);
        feedback =feedbackService.idqueryfeedback(feedback);
        session.setAttribute("feedback",feedback);
        request.getRequestDispatcher( "../page/feedback-edit.html" ).forward(request,response);
    }

    @GetMapping("idquerysession")
    public Map<String,Object> idquerysession(HttpServletRequest request){
        HttpSession session = request.getSession();
        Feedback feedback =(Feedback)session.getAttribute("feedback");
        result.put("feedback",feedback);
        return result;
    }

    @ApiOperation("意见回复")
    @RequestMapping("updatefeedback")
    public  Map<String,Object> UpdateFeedback(@RequestBody Feedback feedback, HttpSession session, HttpServletRequest request, HttpServletResponse response) {
        feedback.setResponse(1);
        Date now = new Date();
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String repletime = dateFormat.format( now );
        feedback.setRepletime(repletime);

        Account account=new Account();
        account.setOpenId(feedback.getOpenid());
        account=feedbackService.selemoneycount(account);

        String handsel = feedback.getHandsel();
        if (!StringUtils.isEmpty(handsel)){ //赠送彩金
            Double feedbackMoney=Double.parseDouble(handsel);
            Double countmoneyBalance=account.getAccountMoneyBalance()+feedbackMoney;

            account.setAccountMoneyBalance(countmoneyBalance);

            Earnings earnings=new Earnings(); //收益记录
            earnings.setNum(feedbackMoney);
            earnings.setOpenid(account.getOpenId());
            earnings.setAccountId(account.getAccountId());
            earnings.setName(account.getAccountName());
            earnings.setSource("反馈收益");
            earnings.setState("已到账");
            earnings.setEarningsExplain("环保金赠送");

            feedbackService.updatemoney(account);      //修改账户总额
            feedbackService.addearnings(earnings);  //新增反馈彩金
        }

        int count =feedbackService.updatefeedback(feedback);
        if (count==1){
            result.put("result","success");
        }else{
            result.put("result","error");
        }
        return result;
    }

}

